package com.ecar.api.websocket;

import com.ecar.api.support.dto.WsPayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

/**
 * 处理客户端发出的 websocket 通信
 * Created by admin
 */
@Controller
@MessageMapping(Topics.TOPIC)
public class WebSocketManager {

	private static final Logger log = LoggerFactory.getLogger(WebSocketManager.class);

	public static SimpMessagingTemplate simpMsg;

	@Autowired
	public WebSocketManager(SimpMessagingTemplate simpMsg) {
		WebSocketManager.simpMsg = simpMsg;
	}

	public static void send(WsPayload wsPayload) {
		simpMsg.convertAndSend(wsPayload.getTopic(), wsPayload.getMsg());
	}

	/**
	 * 默认订阅topic处理
	 * @param message 
	 */
	@MessageMapping(Topics.TOPIC_DEFAULT)
	public void defaultTopic(String message) {
		log.info(message);
	}
}
